using System;
using System.Text;
using System.IO;

using Jeffrey.XFramework;
using Jeffrey.XFramework.XCrawl;
using Jeffrey.XFramework.XNet;

namespace Jeffrey.XProcess
{
    public class FetchImages : Fetcher
    {
        public FetchImages(String name, String description, String classname, String path)
            : base(name, description, classname, path) { }
        public FetchImages(String name, String description, String classname, String path, Filter filter)
            : base(name, description, classname, path, filter) { }

        protected override void Fetch(CrawlUri curi, ref XWebRequest xRequest)
        {
            XWebResponse xResponse = null;

            //Post request to the HTTP Sever
            xRequest = XWebRequest.Create(curi, xRequest, this.m_Controller);

            xRequest.HttpHeaders["Accept"] = "*/*";
            xRequest.HttpHeaders["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 2.0.50727)";
            xRequest.Method = "GET";

            //Get the response from Sever
            xResponse = xRequest.GetResponse();

            if (curi.Status != CrawlUri.StatusCode.S_Succeed)
            {
                curi.Content = String.Empty;
                return;
            }
            //this return code must not include a message-body...
            if ( curi.ResponseCode < 200  ||  //response code: 1xx(information) note: including the default value 0.
                 curi.ResponseCode == 204 ||  //204 (no content)
                 curi.ResponseCode == 304     //304 (not modified)
               )
            {
                /*
                 * do not need to get the content of the page...
                 * just return...
                 */
                return;
            }

            //Get the entity body...
            curi.Content = xResponse.GetContentString();

            //if error...
            if (curi.Status != CrawlUri.StatusCode.S_Succeed)
            {
                curi.Content = String.Empty;
                return;
            }
            else
            {
                /*
                 * Check for http-page redirection...
                 */
                if ( curi.ResponseCode == 201 ||
                     curi.ResponseCode == 301 ||
                     curi.ResponseCode == 302 ||
                     curi.ResponseCode == 303 ||
                     curi.ResponseCode == 307
                    )
                {
                    //clear the content and add the target uri as cuir's outlink
                    curi.Content = String.Empty;
                    curi.NormalLinks.Add(xResponse.RedirectedPage);
                }

                //Set images' encoding to ISO-8859-1
                curi.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
                if (CrawlOrder.PrimEncoding.CodePage != curi.ContentEncoding.CodePage)
                {
                    //ReEncoding opt.
                    Byte[] bytes = CrawlOrder.PrimEncoding.GetBytes(curi.Content);
                    curi.Content = curi.ContentEncoding.GetString(bytes);
                }
            }
            /*
             * reach here means that the page was gotten successfully...
             * So do not change the curi.Success flag, keep its value as this Processor
             * began to process it...
             */
        }
    }
}
